<template>
  <a-row style="padding: 10px;">
    <a-col span="24" style="margin: 10px 0">
      <a-button type="primary" @click="modal.visible = true">注册企业</a-button>
    </a-col>
    <a-col span="24">
      <a-table :dataSource="data" :columns="columns" :pagination="pagination" :loading="loading" size="small" bordered
               :rowKey="record => record.tenantId">
        <template slot="operation" slot-scope="text, record, index">
          <div>
            <span>
              <a @click="() => setDefaultTenant(record)" v-if="record.tenantId != $store.state.userInfo.tenantId">设为默认登陆企业</a>
            </span>
          </div>
        </template>
      </a-table>
      <NCPagination :page="pageObj" @changePage="(page)=>{this.pageObj = page}" @searchList="searchList"/>
    </a-col>
    <!-- 注册企业 -->
    <a-modal v-model="modal.visible" title="注册企业">
      <a-form :form="modal.form">
        <a-form-item label="企业名称" :label-col="modal.labelCol" :wrapper-col="modal.wrapperCol">
          <a-input placeholder="请输入企业名称！"
                   v-decorator="['tenantName', {rules: [{ required: true, message: '请输入企业名称！'}]}]"/>
        </a-form-item>
        <a-form-item label="企业税号" :label-col="modal.labelCol" :wrapper-col="modal.wrapperCol">
          <a-input placeholder="请输入企业税号！"
                   v-decorator="['tenantCode', {rules: [{ required: true, message: '请输入企业税号！'}]}]"/>
        </a-form-item>
        <a-form-item label="企业简介" :label-col="modal.labelCol" :wrapper-col="modal.wrapperCol">
          <a-input placeholder="请输入企业简介！"
                   v-decorator="['introduce', {rules: [{ required: true, message: '请输入企业简介！'}]}]"/>
        </a-form-item>
      </a-form>
      <template slot="footer">
        <a-button key="submit" type="primary" :loading="modal.loading" @click="registerTenantOk">
          确定
        </a-button>
        <a-button key="back" @click="modal.visible = false">取消</a-button>
      </template>
    </a-modal>
  </a-row>
</template>

<script>
  import Vue from 'vue'
  import {SELECT_HEAD, SELECT_LEFT, USER_INFO} from '../store/mutation-types'

  export default {
    name: 'MyTenant',
    data () {
      return {
        pageObj: {total: 10, pageSize: 10, pageNum: 1, pageSizeOptions: ['5', '10', '20', '50', '100']},
        rolePageObj: {total: 10, pageSize: 10, pageNum: 1, pageSizeOptions: ['5', '10', '20', '50', '100']},
        loading: false,
        pagination: false,
        data: [],
        columns: [
          {
            title: '序号',
            width: '20',
            customRender: (text, record, index) => {
              return index + this.pageObj.pageSize * (this.pageObj.pageNum - 1) + 1
            }
          }, {
            title: '企业名称',
            dataIndex: 'tenantName',
            sorter: true
          }, {
            title: '企业税号',
            dataIndex: 'tenantCode',
            sorter: true
          }, {
            title: '企业描述',
            dataIndex: 'introduce',
            sorter: true
          }, {
            title: '创建时间',
            dataIndex: 'createTime',
            sorter: true
          }, {
            title: '状态',
            dataIndex: 'state',
            sorter: true,
            customRender: (text, record, index) => {
              return text === 2 ? '锁定' : '正常'
            }
          }, {
            title: '操作',
            fixed: 'right',
            width: 200,
            scopedSlots: {customRender: 'operation'}
          }
        ],
        modal: {
          labelCol: {span: 7},
          wrapperCol: {span: 12},
          loading: false,
          visible: false,
          form: this.$form.createForm(this)
        }
      }
    },
    mounted () {
      this.searchList()
    },
    methods: {
      searchList () {
        this.loading = true
        this.http.post('upmsApi', '/userCenter/getTenantList', {
          pageNum: this.pageObj.pageNum,
          pageSize: this.pageObj.pageSize,
          data: Vue.ls.get(USER_INFO).id
        }).then((res) => {
          if (res.success) {
            this.data = res.rows
            this.pageObj.total = res.total
          } else {
            this.$message.error(res.message)
          }
          this.loading = false
        })
      },
      // 设置为默认租户
      setDefaultTenant (record) {
        const that = this
        this.$confirm({
          title: '设置并切换默认企业！?',
          onOk () {
            that.http.post('upmsApi', '/userCenter/setDefaultTenant', {tenantId: record.tenantId}).then((res) => {
              if (res.success) {
                that.$store.dispatch('getUserInfo')
                Vue.ls.set(SELECT_HEAD, null)
                Vue.ls.set(SELECT_LEFT, null)
                that.$store.state.selectHead = null
              }
            })
          }
        })
      },
      registerTenantOk () {
        this.modal.form.validateFields((err, values) => {
          if (!err) {
            this.modal.loading = true
            this.http.post('upmsApi', '/userCenter/registerTenant', {
              ...values
            }).then((res) => {
              if (res.success) {
                this.$message.info(res.message)
                this.searchList()
                this.modal.visible = false
              }
              this.modal.loading = false
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
